<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button @click="copyClick('8BCE5C3F12DEA2A92AC91DA65CDA6DA9')">复制</a-button>
        <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增</a-button>
        <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button>
        <j-upload-button type="primary" preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button
            >批量操作
            <Icon icon="mdi:chevron-down"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
      <!--字段回显插槽-->
      <template #htmlSlot="{ text }">
        <div v-html="text"></div>
      </template>
      <template #fileSlot="{ text }">
        <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
        <a-button v-else :ghost="true" type="primary" preIcon="ant-design:download-outlined" size="small" @click="downloadFile(text)">下载</a-button>
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <DeleoAesCipherModal @register="registerModal" @success="handleSuccess"></DeleoAesCipherModal>
  </div>
</template>

<script lang="ts" name="deleoaescipher-deleoAesCipher" setup>
  import { BasicTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage';
  import DeleoAesCipherModal from './components/DeleoAesCipherModal.vue';
  import { columns, searchFormSchema } from './DeleoAesCipher.data';
  import { list, deleteOne, batchDelete, getImportUrl, getExportUrl } from './DeleoAesCipher.api';
  import { downloadFile } from '/@/utils/common/renderUtils';
  import { useClipboard } from '@vueuse/core';
  import { useMessage } from '/@/hooks/web/useMessage';

  const { copy, isSupported } = useClipboard({});
  //注册model
  const [registerModal, { openModal }] = useModal();
  //注册table数据
  const { tableContext, onExportXls, onImportXls } = useListPage({
    tableProps: {
      title: 'Deleo AES Cipher',
      api: list,
      columns,
      canResize: true,
      formConfig: {
        //labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: true,
        fieldMapToNumber: [],
        fieldMapToTime: [],
      },
      actionColumn: {
        width: 120,
        fixed: 'right',
      },
    },
    exportConfig: {
      name: 'Deleo AES Cipher',
      url: getExportUrl,
    },
    importConfig: {
      url: getImportUrl,
      success: handleSuccess,
    },
  });

  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;
  rowSelection.type = 'radio';
  /**
   * 新增事件
   */
  function handleAdd() {
    openModal(true, {
      isUpdate: false,
      showFooter: true,
    });
  }

  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteOne({ id: record.id }, handleSuccess);
  }
  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
  }
  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }
  /**
   * 成功回调
   */
  const message = useMessage();
  function copyClick(record) {
    // const codpData = record.aesText
    //   ?.replaceAll(' ', '')
    //   .match(/.{1,2}/g)
    //   .reduce((pre, item) => {
    //     return pre + String.fromCharCode(parseInt(item, 16));
    //   }, '');
    // if (isSupported) {
    //   copy(codpData);
    //   message.createMessage.success('复制成功：' + codpData);
    // } else {
    //   message.createMessage.error('复制失败，浏览器禁止此网站使用剪贴板功能！');
    // }
    // 获取要复制的文本

    // 创建一个临时的文本输入框
    const codpData = record;
    const tempInput = document.createElement('input');
    tempInput.value = codpData;
    document.body.appendChild(tempInput);

    // 选中临时文本输入框的内容
    tempInput.select();
    tempInput.setSelectionRange(0, 99999); /* 兼容移动端 */

    // 执行复制命令
    document.execCommand('copy');

    // 移除临时文本输入框
    document.body.removeChild(tempInput);

    // 提示复制成功
    message.createMessage.success('复制成功：' + codpData);
  }
  /**
   * 操作栏
   */
  function getTableAction(record): any[] {
    return [
      {
        label: '复制',
        type: 'primary',
        onclick: copyClick.bind(null, record),
      },
      {
        label: '删除',
        type: 'danger',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
          placement: 'left',
        },
      },
    ];
  }
</script>

<style scoped></style>
